package cn.victor.service.impl.dubbo.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.net.Inet4Address;
import java.net.UnknownHostException;

@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER})
public class TransportIPFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println("IpFileter:--------------------------------------------------");
        if (RpcContext.getContext().isConsumerSide()) {
            try {
                RpcContext.getContext().setAttachment("ip", Inet4Address.getLocalHost().getHostAddress());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("request from " + RpcContext.getContext().getAttachment("ip"));
        }
        return invoker.invoke(invocation);
    }

    public static void main(String[] args) throws UnknownHostException {
        System.out.println(Inet4Address.getLocalHost().getHostAddress());
    }
}
